package com.cloudcentral.util;

import com.alibaba.fastjson.JSON;
import com.cloudcentral.entity.User;
import com.cloudcentral.exception.InvalidException;
import com.cloudcentral.mapper.UserBaseMapper;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.PrintWriter;
import java.net.URI;

/**
 * Create by Administrator
 * 2023/10/13 16:26
 */
@Slf4j
public class WebUtils {
  private WebUtils() {
  }

  /**
   * URL前缀
   *
   * @return
   */
  public static String urlPrefix() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    URI uri = URI.create(request.getRequestURL().toString());
    String scheme = uri.getScheme(), authority = uri.getAuthority();
    return StringUtils.join(scheme, "://", authority, "/");
  }

  /**
   * 当前用户名称
   *
   * @return
   * @throws InvalidException
   */
  public static String currentName() throws InvalidException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) throw new InvalidException();
    else return authentication.getName();
  }

  /**
   * 当前用户
   *
   * @return
   * @throws InvalidException
   */
  public static User currentUser() throws InvalidException {
    String name = currentName();
    if (StringUtils.isBlank(name)) throw new InvalidException();
    else {
      UserBaseMapper userBaseMapper = SpringContextUtils.getBean("userBaseMapper", UserBaseMapper.class);
      User user = userBaseMapper.selectUser(name);
      if (user == null) throw new InvalidException("用户不存在");
      else return user;
    }
  }

  /**
   * 返回json数据
   *
   * @param response
   * @param object
   */
  public static void writeJson(HttpServletResponse response, int status, Object object) {
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    response.setContentType("application/json;charset=UTF-8");
    response.setStatus(status);
    PrintWriter out = null;
    try {
      String data = object instanceof String ? (String) object : JSON.toJSONString(object);
      out = response.getWriter();
      out.print(data);
      out.flush();
    } catch (Exception e) {
      log.error("写入返回JSON失败", e);
    } finally {
      if (out != null) out.close();
    }
  }

  /**
   * 返回json数据
   *
   * @param response
   * @param object
   */
  public static void writeJson(HttpServletResponse response, Object object) {
    writeJson(response, HttpServletResponse.SC_OK, object);
  }

  /**
   * 返回json数据
   *
   * @param response
   * @param object
   */
  public static void writeJson(ServletResponse response, Object object) {
    if (response instanceof HttpServletResponse) writeJson((HttpServletResponse) response, object);
  }
}